﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrackSystem.Entities.RequestFeatures
{
    public static class PagedExtensions
    {
        public static PagedList<TSource>  ToPagedList<TSource>(this IEnumerable<TSource> source, int pageNumber, int pageSize)
        {
            if (source == null)     throw new ArgumentNullException(nameof(source));
            if (pageNumber < 1)     throw new ArgumentOutOfRangeException(nameof(pageNumber), "Page number must be greater than 0.");
            if (pageSize < 1)       throw new ArgumentOutOfRangeException(nameof(pageSize), "Page size must be greater than 0.");

            var totalCount = source.Count();
            var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
            return new PagedList<TSource>(items, totalCount, pageNumber, pageSize);
        }
    }
}
